// Modules to control application life and create native browser window
const { app, BrowserWindow } = require("electron");
const path = require("node:path");

// app.commandLine.appendSwitch('allow-file-access');
//关闭 同源策略
// app.commandLine.appendSwitch('disable-web-security');
//禁用站点隔离
// app.commandLine.appendSwitch('disable-site-isolation-trials');
//最小化后，继续运行
// app.commandLine.appendSwitch('disable-background-timer-throttling');

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      webSecurity: false,
      allowDisplayingInsecureContent: true,
      allowRunningInsecureContent: true,
      //partition: 'user',//session user
      //nodeIntegrationInWorker: true,
      //zoomFactor: 0.5,
      //开启node
      nodeIntegration: true,
      contextIsolation: false,
      //开启remote
      enableRemoteModule: true,
      //额外加载脚本(dom解析前)
      webviewTag: true,
      // 支持多线程
      nodeIntegrationInWorker: true,
      //放开权限
      nodeIntegrationInSubFrames: true,
      //preload: et.app.getAppPath() + '\\preload.js'
      allowMediaDevices: true,
    },
  });

  // 获取当前窗口的会话对象
  const session = mainWindow.webContents.session;
  // 设置权限检查处理程序
  session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
    // if (permission === 'geolocation') {
    //   // 假设只有特定来源的请求被允许获取地理位置
    //   if (requestingOrigin === 'https://allowed-origin.com') {
    //     return true;
    //   } else {
    //     return false;
    //   }
    // }
    // 对于其他权限，默认允许
    console.log("permission1：", permission, requestingOrigin)
    return true;
  });

  // 设置权限请求处理程序
  session.setPermissionRequestHandler((webContents, permission, callback) => {
    // 在这里处理权限请求的用户界面逻辑，根据需要决定是否允许权限
    // 示例：总是允许权限请求
    console.log("permission2： ", permission, )
    callback(true);
  });

  // and load the index.html of the app.
  // mainWindow.loadFile('index.html')
  mainWindow.loadURL("https://h5.dj7r.com");

  // Open the DevTools.
  // mainWindow.webContents.openDevTools();
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
